#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include <malloc.h>
#define n 5
typedef int SLDataType;
typedef struct SeqList
{
	SLDataType* a;
	int length;
}SL;

int main()
{
	SL L1;
	
	L1.length = n;
	L1.a = (SLDataType*)malloc(sizeof(SLDataType) * L1.length);
	SL L2;
	L2.length = n;
	L2.a = (SLDataType*)malloc(sizeof(SLDataType) * L2.length);
	for (int i = 0; i < L1.length; ++i)
	{
		scanf("%d", &L1.a[i]);
	}
	int j = 0;
	int i = 0;
	for (i = 0; i < L1.length; i++)
	{
		if (L1.a[i] % 3 == 0)
		{
			L2.a[j++] = L1.a[i];
			L1.a[i] = -1;
		}
	}
	for (j = i; i >= 0; i--)
	{
		if (L1.a[i] != -1)
		{
			L2.a[j--] = L1.a[i];
		}
	}

	for (int i = 0; i < L2.length; ++i)
	{
		printf("%d ", L2.a[i]);
	}
	return 0;
}